<?
include('wallet_db.html');
echo "<script language='JavaScript'>
    <!--
    var imgDir = 'images/';
    var preloadImages = new Array();
    preloadImages[0] = new Image();
    preloadImages[0].src = imgDir + 'spacer.gif';
    preloadImages[1] = new Image();
    preloadImages[1].src = imgDir + 'callout_a_1.gif';
    preloadImages[2] = new Image();
    preloadImages[2].src = imgDir + 'callout_a_2.gif';
    preloadImages[3] = new Image();
    preloadImages[3].src = imgDir + 'callout_a_3.gif';
    preloadImages[4] = new Image();
    preloadImages[4].src = imgDir + 'callout_a_4.gif';
   	//-->
</script>";
$params[sid] = $sid;
$params[action] = "moveClient2Plan";
$url = "$baseURL/$webDIR/?".encode_params($params);
unset($params);
$params[sid] = $sid;
$params[action] = "createPlan";
$createplanurl = "$baseURL/$webDIR/?".encode_params($params);
#$choose_client=translate('choose_client');
$dberror=translate('dberror');
$next_step=translate('next_step');
$select_user=translate('select_user');
#$message_upgr_success=translate('message_upgr_success');
$no_clients=translate('no_clients');
$no_client_selected=translate('no_client_selected');
$legend=translate('legend');
$step_1_message_1=translate('step_1_message_1');
$step_1_message_2=translate('step_1_message_2',array('url' => "$createplanurl"));
$step_1_message_3=translate('step_1_message_3');
$step_2_message_1=translate('step_2_message_1');
$step_3_message_1=translate('step_3_message_1');
$img_txt_1=translate('img_txt_1');
$img_txt_2=translate('img_txt_2');
$img_txt_3=translate('img_txt_3');
if (!isset($step2) && !isset($step3)) {
	//-----------------------------------------------------
	        $sql['type'] = "SELECT";
	        $sql['params'] = "client_id, username, blocked";
	        $sql['table'] = "clients";
	        $sql['where'] = "reseller_id='$id'";
	        $sql['options'] = "ORDER BY username";
	        $sql['return'] = "struct";
        //------------------------------------------------------
        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
                exit("<b>$no_clients</b>");
        }
        foreach ($result as $value) {
                $clients[$value[client_id]]=$value[username];
        }
        echo "<form action='$url' method='POST'>
                <div align=left>".$step_1_message_1.$step_1_message_2.$step_1_message_3;
		echo "<div align=center>
                        <table border='0' cellspacing='0' cellpadding='5'>
                        <tr>
                                <td valign='middle' align='center'>
                                        <img src='images/ico_step_1.gif' border='0'>
                                </td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
				<td>&nbsp;</td>
                                <td>&nbsp;</td>
                        </tr>
                        <tr>
                                <td valign='middle' align='center'>
                                        <img src='images/ico_current_step.gif' border='0'>
                                </td>
                                <td valign='middle' align='center'><font color='#FF6000'>$img_txt_1</font></td>
                                <td valign='middle' align='center'><img src='images/ico_next_step.gif' border='0'></td>
                                <td valign='middle' align='center'>$img_txt_2</td>
					<td valign='middle' align='center'><img src='images/ico_next_step.gif' border='0'></td>
                                <td valign='middle' align='center'>$img_txt_3</td>
                        </tr>
                        </table>
                        </div><br><br>";
                echo "$select_user&nbsp;<select name='client_id'>
                <option value=0>----------</option>\n";
                echo "<optgroup label='Active'>";
        		foreach ($result as $key => $val) {
            		if ($val['blocked'] == 0) {
        				echo "<option value='".$val['client_id']."'>".$val['username']."</option>\n";
            		}
        		}
        		echo "</optgroup>
        		<optgroup label='Suspended (traffic)'>";
        		unset($key,$val);
        		foreach ($result as $key => $val) {
        			if ($val['blocked'] == 2) {
        				echo "<option value='".$val['client_id']."'>".$val['username']."</option>\n";
        			}
        		}
        		echo "</optgroup>
        		<optgroup label='Suspended (expired)'>";
        		unset($key,$val);
        		foreach ($result as $key => $val) {
        			if ($val['blocked'] == 3) {
        				echo "<option value='".$val['client_id']."'>".$val['username']."</option>\n";
        			}
        		}
        		echo "</optgroup>";
        echo "</select>
	<input type=submit name=submit value='$next_step'>
                <input type='hidden' name='step2' value='345'></form></div>";
} elseif (isset($step2) && $step2 == '345') {
	if (empty($client_id) or !isset($client_id)) {
		echo "<div class=errormsg>$no_client_selected</div>";
	} else {
		#$client_id = $_REQUEST['client_id'];
		//-----------------------------------------------------
                $sql['type'] = "SELECT";
                $sql['params'] = "cl.username, (cl.firstname||' '||cl.lastname) AS name, cl.server, cl.blocked, pr.name AS prname, pr.wholesale,pr.product_id, pu.start_date, pu.expire_date";
                $sql['table'] = "purchases AS pu, products AS pr, clients AS cl";
                $sql['where'] = "pu.client_id = '$client_id' AND pu.client_id=cl.client_id AND pu.product_id = pr.product_id AND pr.type = 'plan'";
                $sql['options'] = "";
                $sql['return'] = "struct";
                #$sql['return'] = "query";
	        //------------------------------------------------------
	        #exit($result=sql_exec($sql,$dbh81,'assoc'));
	        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	                exit($dberror);
	        }
		#exit(print_r($result));
		if ($result[0]['blocked'] != 0) {
			if ($result[0]['blocked'] == 2) {
				echo "<p>Client suspended - traffic</p>";
			} elseif ($result[0]['blocked'] == 3) {
				echo "<p>Client suspended - expired</p>";
				$params['sid'] = $sid;
				$params['action'] = "moveClient2Plan";
				$params['step2'] = 345;
				$params['client_id'] = $client_id;
				$ret_url = "$baseURL/$webDIR/?".encode_params($params);

				$params['sid'] = $sid;
				$params['action'] = "manualUpgrades";
				$params['type'] = "renewal";
				$params['client_id'] = $client_id;
				$params['returnURL'] = $ret_url;
				$params['hid_var'] = 345;
				$urla = "$baseURL/$webDIR/?".encode_params($params);
				echo "<p><a href='$urla'>Renew</a></p>";
			}
		} else {
			$result = $result[0];
			$start_date = date("d-M-Y",strtotime($result['start_date']));
			$expire_date = date("d-M-Y",strtotime($result['expire_date']));
			$wholesale = $result['wholesale'][12]['USD'];
			$user_name = $result['username'];
			$cl_name = $result['name'];
			$cl_server = $result['server'];
			$plan_name = $result['prname'];
			$product_id = $result['product_id'];
			unset($params);
			$params[sid] = $sid;
			$params[action] = "comparePlans";
			$params[current_plan] = $product_id;
			$params['from_page'] = 'moveClient2Plan';
			$compareplanurl = "$baseURL/$webDIR/?".encode_params($params);
			$upgrade_plan = translate('upgrade_pl');
			$user_upgrade = translate('user_upgrade',array('username' => "$user_name"));
			$no_upgrades = translate('no_upgrades',array('username' => "$user_name"));
			$selected_user = translate('selected_user');
			$uname = translate('username');
			$curr_plan = translate('curr_plan');

			$expiration_date_th = translate('expire_date');
			$showdomains = translate('show_domains',array('username' => "$user_name",
									'name' => "$cl_name"));
			$step_2_message_2=translate('step_2_message_2',array('username' => "$user_name",
									'account_owner_full_name' => "$cl_name",
									'server' => "$cl_server",
									'url' => "$createplanurl",
									'compare_url' => "$compareplanurl"));
			echo "<script language='JavaScript'>
			<!--
			var myWin = '';
			function openWin(url, name, properties) {
	    	    if (myWin && myWin.open && !myWin.closed) {
			        myWin.close();
	    	    }
    		    myWin = window.open(url, name, properties);
    		    return false;
        	}
			//-->
			</script>";
			echo "<div align='left'>".$step_2_message_1.$step_2_message_2;
			echo "<div align=center>
        	                <table border='0' cellspacing='0' cellpadding='5'>
        	                <tr>
					<td>&nbsp;</td>
        	                        <td>&nbsp;</td>
        	                        <td valign='middle' align='center'>
        	                                <img src='images/ico_step_2.gif' border='0'>
        	                        </td>
        	                        <td>&nbsp;</td>
						<td>&nbsp;</td>
        	                        <td>&nbsp;</td>
        	                </tr>
        	                <tr>
        	                        <td valign='middle' align='center'>
        	                                <img src='images/ico_completed_step.gif' border='0'>
        	                        </td>
        	                        <td valign='middle' align='center'>$img_txt_1</td>
        	                        <td valign='middle' align='center'><img src='images/ico_current_step.gif' border='0'></td>
        	                        <td valign='middle' align='center'><font color='#FF6000'>$img_txt_2</font></td>
					<td valign='middle' align='center'><img src='images/ico_next_step.gif' border='0'></td>
					<td valign='middle' align='center'>$img_txt_3</td>
        	                </tr>
        	                </table>
        	                </div><br><br>";
			echo "<TABLE width='100%' border='0' cellspacing='0' cellpadding='0'>
			<tr>
				<td valign='top'>";
			// user details & current user plan
			echo "<TABLE width='300' class='bTable' cellspacing=0>
			<tr>
				<th>$selected_user</th>
				<th>$uname</th>
				<th>$curr_plan</th>
			</tr>
			<tr>
				<td align='center'><b>$cl_name</b></td>
				<td align='center'><b>$user_name</b></td>
				<td align='center'><b>$plan_name</b></td>
			</tr>

			<tr>

				<td align='center' colspan='3'><b>$expiration_date_th - $expire_date</b></td>

			</tr>
			</table><br>";
			unset($params);
			$params[sid] = $sid;
			$params[action] = "showDomains";
			$params[client_id] = $client_id;
			$showdomainurl = "$baseURL/$webDIR/?".encode_params($params);
			echo "<center><a href='#' onClick=\"return openWin('$showdomainurl','domain_details','width=850, height=450, scrollbars=yes')\">
			$showdomains</a></center><br><br>";
			print_plan_properties($product_id);
			echo "</td>
			<td valign='top'>";
			// current plan details
			#$current_expire_date = get_plan_expire_date($client_id);
			$period_left = round( (strtotime($expire_date)-time())/(3600*24*31));
			$amount_to_return = $wholesale*$period_left/12;

			// plans to upgrade
		        //-----------------------------------------------------
		                $sql['type'] = "SELECT";
		                $sql['params'] = "product_id,name,wholesale";
		                $sql['table'] = "products";
		                $sql['where'] = "reseller_id='$id' AND type='plan'";
		                $sql['options'] = "";
		                $sql['return'] = "struct";
		        //------------------------------------------------------
		        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		                exit($dberror);
		        }
			$params[sid] = $sid;
			$params[action] = "moveClient2Plan";
			$url = "$baseURL/$webDIR/?".encode_params($params);
			echo "<script language='JavaScript'>
			<!--
			var baa = new Array();";
			$i=0;
			foreach ($result as $val) {
				echo "baa['".$i."']=".$val['product_id'].";\n";
				$i ++;
			} ?>
			var bavCurrent = null;
			function bafSL(id) {
				if (bavCurrent == id) return;
				document.getElementById('paf_'+id).style.display='block';
				if (bavCurrent != null) document.getElementById('paf_'+bavCurrent).style.display='none';
				document.getElementById("wrapper_" + id).className = "boldMe";
    	       	if (bavCurrent) document.getElementById("wrapper_" + bavCurrent).className = "unBoldMe";

				bavCurrent=id;

			}
			function bafSLS() {
				for (i=0;i<baa.length;i++) {
					if (document.getElementById(baa[i])) {
						if (document.getElementById(baa[i]).checked) {
							document.getElementById('paf_'+baa[i]).style.display='block';
							return;
						}
					}
				}
			}
			//-->
			</script>
			<?
			echo "<script type='text/javascript' src='overlib.js'></script>
			<div id='overDiv' style='position:absolute; visibility:hidden; z-index:1000; background-color: none !important;'></div>
			<TABLE width='250' class='bTable' cellspacing=0>
			<form action='$url' method='POST'>
			<tr>
				<th colspan=2>$user_upgrade</th>
			</tr>";
			$noother=0;
			foreach ($result as $value) {
				if ($value['wholesale'][12]['USD'] > $wholesale) {
					$total_price = sprintf("%01.2f",($value['wholesale'][12]['USD']*$period_left/12 - $amount_to_return));

					$display_mo_price = sprintf("%01.2f",$total_price/$period_left);
					if ($period_left < 1 && $period_left >0) {
							$period_left = sprintf("%01.2f",$period_left);
					}
					$div_content = $period_left;
					if ($period_left == 1) {
						$div_content .= " <a href='#' onmouseover='overlib(\"time remining untill expiration date\",
						CAPTION, \"month\");' onmouseout='return nd();'
						onClick='return false;'>month</a> ";
					} else {
						$div_content .= " <a href='#' onmouseover='overlib(\"time remining untill expiration date\",
						CAPTION, \"months\");' onmouseout='return nd();'
						onClick='return false;'>months</a> ";
					}

					$mess_wp_m = sprintf("%01.2f",$value['wholesale'][12]['USD']/12);
					$tmp = sprintf("%01.2f",$wholesale/12);
					$mesg = "($mess_wp_m x $period_left) - ($tmp x $period_left) = $total_price<br>";
					$mesg .= "Pokazanata cena se poluchava kato se umnozhi mesecnata cena na izbraniq plan po ostavashtite meseci do iztichane na nastoyashtia plan, i ot neya se izvadi umnozhenata po ostavashtia period (v meseci) mesechna cena na nastoqshtiq plan, koqto e zaplatena pri plashtaneto za nastoqshtiq hosting plan";

					$div_content .= "x <a href='#' onmouseover='overlib(\"$mesg\",
						CAPTION, \"ATTENTION!\");' onmouseout='return nd();'
						onClick='return false;'>\$$display_mo_price</a> =
						<a href='#' onmouseover='overlib(\"total price to be payed\",
						CAPTION, \"Price\");' onmouseout='return nd();'
						onClick='return false;'>\$$total_price USD</a>";
					unset($detailsurl,$params);
					$params['sid'] = $sid;
					$params['action'] = "showPlanDetails";
					$params['rid'] = $id;
					$params['pid'] = $value['product_id'];
					$params['from_page'] = 'moveClient2Plan';
					$params['username'] = $user_name;
					$params['current_plan'] = $product_id;
					$params['product'] = '12'."|".$product_id;
					$params['current_expire'] = $expire_date;
					$params['current_plan_name'] = $plan_name;
					$params['period_left'] = $period_left;
					$params['amount_to_return'] = $amount_to_return;
					$detailsurl = "$baseURL/$webDIR/?".encode_params($params);
					echo "<tr>
					<td width='10'><input type='radio' id='".$value['product_id']."' name='product' value='12|".$value['product_id']."'
						onClick=\"bafSL(this.id);\"></td><td width='240' onClick=\"document.getElementById('".$value['product_id']."').click();\">
							<a href='#' onClick=\"return openWin('$detailsurl','PlanDetails',
							'width=650, height=650, scrollbars=yes');\"><span  id=\"wrapper_".$value['product_id']."\">".
						$value['name']."</span></a><div id='paf_".$value['product_id']."'
						style='display:none'>$div_content</div></td>
					</tr>";
					$noother=1;
				}
			}
			if ($noother == 0) {
				echo "<tr><td colspan=2><b>$no_upgrades</b></tr></form>";
			} else {
				echo "<tr>
				<td colspan=2 align='center'><input type='submit' name='submit' value='$upgrade_plan'>
				<input type='hidden' name='action' value='total'>
				<input type='hidden' name='type' value='plan'>
				<input type='hidden' name='user_name' value='$user_name'>
				<input type='hidden' name='current_expire' value='$expire_date'>
				<input type='hidden' name='current_plan_name' value='$plan_name'>
				<input type='hidden' name='period_left' value='$period_left'>
				<input type='hidden' name='amount_to_return' value='$amount_to_return'>
				<input type='hidden' name='step3' value='785'>

				</td>
				</tr>
				</form>
				";
			}
			echo "</table><br>
			<center><a href='#' onClick=\"return openWin('$compareplanurl','Compare_Plans','width=650, height=550, scrollbars=yes');\">".translate('compare_link')."</a></center>";
			/*if ($noother !=0) {
				echo "<br><br><br><br>
				<center><input type='submit' name='submit' value='$upgrade_plan'></center>
				</form>";
			}*/
			echo "<script language='JavaScript'>
			<!--
			bafSLS();
			//-->
			</script>";
			/* echo "<br><br>
			<TABLE class='bTable' cellspacing=0>
				<tr>
					<th>Bosses' Table</th>
				</tr>
				<tr>
					<td>&nbsp;</td>
				</tr>
			</table>"; */
			echo "</td>
			</tr>
			</table>";
		}
	}
} elseif (isset($step3) && $step3 == '785') {

	$uname_th = translate('username');
	$curr_plan_th = translate('curr_plan');
	$new_plan_th = translate('new_plan');
	$expire_date_th = translate('expire_date');
	$amount_th = translate('amount');
	$summary_th = translate('summary');
	$prod_id = explode("|",$product);
	$prod_id = $prod_id[1];
	//-----------------------------------------------------
		$sql['type'] = "SELECT";
		$sql['params'] = "name,wholesale";
		$sql['table'] = "products";
		$sql['where'] = "product_id = '$prod_id'";
		$sql['options'] = "";
		$sql['return'] = "struct";
	//------------------------------------------------------
	if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		exit($dberror);
	}
	$new_plan_name = $result[0]['name'];
	$wholesale = $result[0]['wholesale'][12]['USD'];
	$total_price = sprintf("%01.2f",($wholesale*$period_left/12 - $amount_to_return));

	echo "<div align=left>".$step_3_message_1."</div>";
	echo "<div align=center>
	<table border='0' cellspacing='0' cellpadding='5'>
	    <tr>
		<td>&nbsp;</td>
            	<td>&nbsp;</td>
		<td>&nbsp;</td>
            	<td>&nbsp;</td>
            	<td valign='middle' align='center'>
	            <img src='images/ico_step_3.gif' border='0'>
            	</td>
            	<td>&nbsp;</td>
        </tr>
        <tr>
            <td valign='middle' align='center'>
				<img src='images/ico_completed_step.gif' border='0'>
            </td>
            <td valign='middle' align='center'>$img_txt_1</td>
            <td valign='middle' align='center'><img src='images/ico_completed_step.gif' border='0'></td>
            <td valign='middle' align='center'>$img_txt_2</td>
			<td valign='middle' align='center'><img src='images/ico_current_step.gif' border='0'></td>
			<td valign='middle' align='center'><font color='#FF6000'>$img_txt_3</font></td>
        </tr>
     </table>
     </div><br><br>
     <table border='0' width='500' class='bTable'>
     <tr>
     	<th colspan='5'>$summary_th</th>
     </tr>
     <tr>
     	<th>$uname_th</th>
     	<th>$curr_plan_th</th>
     	<th>$new_plan_th</th>
     	<th>$expire_date_th</th>
     	<th>$amount_th</th>
     </tr>
     <tr>
     	<td align='center'>".$user_name."</td>
     	<td align='center'>".$current_plan_name."</td>
     	<td align='center'>".$new_plan_name."</td>
     	<td align='center'>".$current_expire."</td>
     	<td align='right'>$".$total_price."</td>
     </tr>
     </table><br><br>
	";
	#print_r($_POST);
	//------------------------------------------------------
        $sql['type'] = "SELECT";
        $sql['params'] = "a.expire_date";
        $sql['table'] = "purchases as a,clients as b, products as c";
        $sql['where'] = "a.client_id=b.client_id and b.reseller_id='$id' and b.username='$user_name' and a.product_id=c.product_id and c.type='plan'";
        $sql['options'] = "";
        $sql['return'] = "struct";
        #$sql['return'] = "query";
        //------------------------------------------------------
        #exit($result=sql_exec($sql,$dbh81,'assoc'));
        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		exit('ERROR');
        } else {
                $expire_date=$result[0][expire_date];
        }
        $url1='https://supremeserver1.com/resellers/u/upgrade/upgrade.php?';
        $data[type]=$_POST[type];
        $data[action] = $_POST[type];
        $data[username]=$user_name;
        #$data[return_url]="$urla";
        $data[return_url]='';
        $data[expiration_date]=$expire_date;
        $data[reseller_authenticated]="yes";
        if (isset($_POST[action])) {
                $data=array_merge($_GET,$_POST);
		$data['username'] = $user_name;
		unset($data['user_name']);
                $data[reseller_authenticated]="yes";
                $data[wal_proc]='yes';
        }
        $data[returnURL]="$urla";
        if(is_array($data)){
                while(list($k,$v) = each($data)){
                        if($v){
                                if(is_array($v)){
                                        while(list($k1,$v1) =each($v)){
                                                 if($v1) $options .= "&$k"."[".$k1."]=".urlencode($v1);
                                        }
                                }else {
                                        $options .= "&$k=".urlencode($v);
                                }
                        }
                }
                $url1 = $url1.$options;
        }
#        if (is_me()) echo "<br><br>$url1<br>";
        include($url1);
}
?>
